package com.example.shengyin.frontController;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Article;
import com.example.shengyin.entity.ArticleSimple;
import com.example.shengyin.entity.Page;
import com.example.shengyin.service.ArticleService;
import com.example.shengyin.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("/front/aboutus")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api(tags = "FrontAboutUsController", description = "关于我们")
public class FrontAboutUsController
{
    @Autowired
    private ArticleService service;
    @Autowired
    private RedisTemplate redisTemplate;
    public Map ReMap(){
        Map<String,Object> map=new HashMap<>();
        map.put("公司简介 ComProfile",new ArrayList<>(Arrays.asList("CompanyInfo")));
        map.put("媒体报道 Media",new ArrayList<>(Arrays.asList("媒体报道 Media")));
        map.put("交流合作 Cooperate",new ArrayList<>(Arrays.asList("交流合作")));
        map.put("大事记 Event",new ArrayList<>(Arrays.asList("大事记")));
        map.put("公司新闻 Comnews",new ArrayList<>(Arrays.asList("开业活动 Opening","国际交流 International","员工活动   \n" +
                "Staff","亲子活动  \n" +
                "Parent-child","公益讲座 Public","公益活动 Interest","家长培训Training","社会关注 Social ","全部 null")));
        map.put("活动讲座 Lecture",new ArrayList<>(Arrays.asList("热门推荐","专题精选","门店信息")));
        map.put("言语科普 Speech",new ArrayList<>(Arrays.asList("言语科普")));
        map.put("视频中心","企业活动 Corporate  品牌宣传 Brand  家庭训练技巧 Family  朗朗 Lang");
        return map;
    }


    @ApiOperation("根据标题关键词查找")
    @PostMapping(value = "/selectword")
    public Result select2(@ApiParam(name = "typecode",value = "种类，例如：媒体报道、交流合作") @RequestParam(required = true) String typecode,
                          @ApiParam(name = "name",value = "关键词") @RequestParam(required = true) String name)
    {
        List<Article> article = service.selectByNmae(name,typecode,null);
        System.out.println(JSON.toJSONString(article));
        if(article==null)
            return Result.error("没有找到数据");
        return Result.success(article);
    }


    @ApiOperation("查看详细的内容")
    @GetMapping(value = "/finddetail/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        Article article = service.selectById(id);
        System.out.println(JSON.toJSONString(article));
        if(article==null)
            return Result.error("没有找到数据");
        article.setVisittimes(article.getVisittimes()+1);
        service.update(article);
        return Result.success(article);
    }

    @ApiOperation("关于我们->公司简介")
    @GetMapping(value = "/findComProfile")
    public Result findComProfile()
    {
        List<Article> articles = service.articleList(new Page(1, 1), "公司简介", null, null, null, null);
        if(articles!=null)
        return Result.success(articles.get(0));
        return Result.success("没有找到");
    }
    @ApiOperation("关于我们->媒体报道")
    @GetMapping(value = "/findMedia/{page}/{limit}")
    public Result findMedia(@PathVariable(value = "page") int page,
                            @PathVariable(value = "limit")int limit)
    {
        Page page1 = new Page(page, limit);
        List<ArticleSimple> articles = service.articleList2(page1, null, "Media", null, null, null);
        int count = service.countTotall(null, "Media", null, null, null);
        if(count==0||articles==null)
        {
            return Result.success(page1,"没有数据");
        }
        page1.setCount(count);
        page1.setData(articles);
        return Result.success(page1);
    }
    @ApiOperation("关于我们->交流合作")
    @GetMapping(value = "/findCooperate/{page}/{limit}")
    public Result findCooperate(@PathVariable(value = "page") int page,
                                @PathVariable(value = "limit")int limit)
    {
        Page page1 = new Page(page, limit);
        List<ArticleSimple> articles = service.articleList2(page1, null, "Cooperate", null, null, null);
        int count = service.countTotall(null, "Media", null, null, null);
        if(count==0||articles==null)
        {
            return Result.success(page1,"没有数据");
        }
        page1.setCount(count);
        page1.setData(articles);
        return Result.success(page1);
    }
    @ApiOperation("关于我们->大事记")
    @GetMapping(value = "/findBigEvent/{page}/{limit}")
    public Result findEvent(@PathVariable(value = "page") int page,
                            @PathVariable(value = "limit")int limit)
    {
        Page page1 = new Page(page, limit);
        List<ArticleSimple> articles = service.articleList2(page1, null, "Event", null, null, null);
        int count = service.countTotall(null, "Media", null, null, null);
        if(count==0||articles==null)
        {
            return Result.success(page1,"没有数据");
        }
        page1.setCount(count);
        page1.setData(articles);
        return Result.success(page1);
    }
}
